package com.google.sydym6.view.fragments;

import com.google.sydym6.R;
import com.google.sydym6.controllers.FavouritesListRadioController;
import com.google.sydym6.controllers.IListRadioController;
import com.google.sydym6.logic.database.data.IRadioData;
import com.google.sydym6.view.activities.RadioPlayerActivity;
import com.google.sydym6.view.adapters.RadioListAdapter;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class FavouritesPageFragment extends Fragment {
  
  static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";
  int backColor;
  
  IListRadioController controller;
  BaseAdapter adapter;
  
  public static FavouritesPageFragment newInstance() {
    FavouritesPageFragment pageFragment = new FavouritesPageFragment();
    Bundle arguments = new Bundle();
    pageFragment.setArguments(arguments);
    return pageFragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    controller = new FavouritesListRadioController(this);
    adapter = new RadioListAdapter(this.getActivity(), R.layout.radio_list_item, controller.getList());
    backColor = Color.argb(255, 0, 0, 255);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.favourites_fragment, null);
    
    view.setBackgroundColor(backColor);
    
    ListView radioFLV = (ListView) view.findViewById(R.id.favouritesRadioList);
    radioFLV.setAdapter(adapter);
    
    radioFLV.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        	controller.setCurrentRadio((IRadioData) adapter.getItem(position));
  			Intent intent = new Intent(parent.getContext(), RadioPlayerActivity.class);
  			//TODO ������ putExtra id
  			intent.putExtra("id", id);
  			startActivity(intent);
        }
      });
      
    return view;
  }
}
